package com.leyou.search.listen;

import com.leyou.search.repository.GoodsRepository;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/21
 * @Time: 16:20
 */
@Component
public class GoodsListener {
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private SearchService searchService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.search.create.queue"),
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE", ignoreDeclarationExceptions = "true", type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}
    ))
    public  void listenCreate(Long id) throws IOException {
        if (id == null || id.longValue() <= 0) {
            return;
        }
        this.searchService.saveIndex(id);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.search.delete.queue"),
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"item.delete"}
    ))
    public  void listenDelete(Long id) throws IOException {
        if (id == null || id.longValue() <= 0) {
            return;
        }
        this.searchService.deleteIndex(id);

    }
}
